package tms.model;

import java.util.Calendar;
import java.util.Date;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;

import tms.service.ServiceHandler;

@XmlRootElement
public class Flight{

    public static final String tableName = "Flight";
    public static String[] columns = {"IDDeparting", "IDArriving", "TotalSeats", "FlightName",
            "IsActive", "Discount", "Price", "Duration", "Price2", "Distance", "IDAircraft",
            "DepDay", "DepMonth", "DepYear", "DepHour", "DepMinute"};

    private int id = -1;
    private int idDeparting = -1;
    private int idArriving = -1;
    private int totalSeats = 0;
    private String flightName = "";
    private boolean isActive = false;
    private double discount = 0;
    private double price = 0;
    private int duration = 0;
    private double price2 = 0;
    private double distance = 0;
    private int idAircraft = -1;

    private int depDay = 1;
    private int depMonth = 1;
    private int depYear = 2013;
    private int depHour = 0;
    private int depMinute = 0;

    private Destination departing = null;
    private Destination arriving = null;
    private Aircraft aircraft = null;

    /**
     * Load Departing destination by IDDeparting.
     */
    private void bindingDeparting(int idDeparting) {
        if (idDeparting > 0) {
            setDeparting(ServiceHandler.getFirstDestination("Destination.ID = " + idDeparting));
        }
   }

    /**
     * Load Arriving destination by IDArriving.
     */
    private void bindingArriving(int idArriving) {
        if (idArriving > 0) {
            setArriving(ServiceHandler.getFirstDestination("Destination.ID = " + idArriving));
        }
   }

    public String[] getValues() {
        return new String[] {getIdDeparting() + "", getIdArriving() + "",
                getTotalSeats() + "", "'" + getFlightName() + "'",
                isActive() == true ? "1" : "0", getDiscount() + "", getPrice() + "",
                getDuration() + "", getPrice2() + "", getDistance() + "", getIdAircraft() + "",
                depDay + "", depMonth + "", depYear + "", depHour + "", depMinute + ""};
   }

    /****************************************************************************/

    public Date getArrivingDate() {

        Calendar c = Calendar.getInstance();
        c.setTime(getDepartureDate());
        c.add(Calendar.HOUR, duration);

        return c.getTime();
   }

    @XmlElement
     public int getId() {
        return id;
   }

    public void setId(int id){
    	this.id = id;
    }
    @XmlElement
    public int getIdDeparting() {
        return idDeparting;
   }

    public void setIdDeparting(int idDeparting) {
        this.idDeparting = idDeparting;
        bindingDeparting(idDeparting);
   }

    @XmlElement
    public int getIdArriving() {
        return idArriving;
   }

    public void setIdArriving(int idArriving) {
        this.idArriving = idArriving;
        bindingArriving(idArriving);
   }

    public Date getDepartureDate() {
        Calendar c = Calendar.getInstance();
        c.set(depYear, depMonth - 1, depDay, depHour, depMinute);
        return c.getTime();
   }

    public void setDepartureDate(Date departureDate) {
        Calendar c = Calendar.getInstance();
        c.setTime(departureDate);
        depDay = c.get(Calendar.DAY_OF_MONTH);
        depMonth = c.get(Calendar.MONTH) + 1;
        depYear = c.get(Calendar.YEAR);
        depHour = c.get(Calendar.HOUR_OF_DAY);
        depMinute = c.get(Calendar.MINUTE);
   }

    @XmlElement
    public int getTotalSeats() {
        return totalSeats;
   }

    public void setTotalSeats(int totalSeats) {
        this.totalSeats = totalSeats;
   }

    @XmlElement
    public String getFlightName() {
        return flightName;
   }

    public void setFlightName(String flightName) {
        this.flightName = flightName;
   }

    @XmlElement
    public boolean isActive() {
        return isActive;
   }

    public void setActive(boolean isActive) {
        this.isActive = isActive;
   }

    @XmlElement
    public double getDiscount() {
        return discount;
   }

    public void setDiscount(double discount) {
        this.discount = discount;
   }

    
    public Destination getDeparting() {
        return departing;
   }

    private void setDeparting(Destination departing) {
        this.departing = departing;
   }

    
    public Destination getArriving() {
        return arriving;
   }

    private void setArriving(Destination arriving) {
        this.arriving = arriving;
   }

    @XmlElement
    public double getPrice() {
        return price;
   }

    public void setPrice(double price) {
        this.price = price;
   }

    @XmlElement
    public int getDuration() {
        return duration;
   }

    public void setDuration(int duration) {
        this.duration = duration;
   }

    @XmlElement
    public double getPrice2() {
        return price2;
   }

    public void setPrice2(double price2) {
        this.price2 = price2;
   }

    @XmlElement
    public double getDistance() {
        return distance;
   }

    public void setDistance(double distance) {
        this.distance = distance;
   }

    @XmlElement
    public int getIdAircraft() {
        return idAircraft;
   }

    public Aircraft getAircraft() {
        return aircraft;
   }

    public void setAircraft(Aircraft aircraft) {
        this.aircraft = aircraft;
   }

    @XmlElement
    public int getDepDay() {
        return depDay;
   }

    @XmlElement
    public int getDepMonth() {
        return depMonth;
   }

    @XmlElement
    public int getDepYear() {
        return depYear;
   }

    @XmlElement
    public int getDepHour() {
        return depHour;
   }

    @XmlElement
    public int getDepMinute() {
        return depMinute;
   }
}
